package auctionhaus

class Listing {
    String name
    Date endTime
    String description
    
    Customer seller
    Customer winner

    Bid startBid
    static hasMany = [bids: Bid]

    static constraints = {
        name maxSize:  64, blank: false
        endTime blank: false, validator: { valDate ->
            return valDate.after(new Date())
        }
        description maxSize: 256, blank: true, nullable: true

        seller blank: false
        winner blank: true, nullable: true

        startBid blank: false
        
        bids validator: { incomingBids ->
            def retVal = true
            if(incomingBids) {
                def allBids = incomingBids.sort {it.dateCreated}.reverse()
                def previous = allBids[0]
                allBids.each {
                    if(it.id != previous.id) {
                        if(it.amount < previous.amount + 0.5) {
                            retVal = false
                        }
                        previous = it
                    }
                }
            }
            return retVal
        }
    }
}
